﻿#include "Measure_Sector.h"
#include "qdebug.h"
Measure_Sector::Measure_Sector()
{


    CameraParameters.Clear();

	CameraParameters[0] = 0;
	/*CameraParameters[1] = -7.10797e-007;
	CameraParameters[2] = 0.0114855;
	CameraParameters[3] = 0.0114894;
	CameraParameters[4] = -136.316;
	CameraParameters[5] = 860.579;
	CameraParameters[6] = 2000;
	CameraParameters[7] = 1500;*/



	CameraParameters.Clear();
	CameraParameters[0] = 0;
	CameraParameters[1] = -0.940741;
	CameraParameters[2] = 1.14751e-005;
	CameraParameters[3] = 1.14709e-005;
	CameraParameters[4] = 891.283;
	CameraParameters[5] = 943.919;
	CameraParameters[6] = 2000;
	CameraParameters[7] = 1500;
   /* CameraParameters[0] = 0;
    CameraParameters[1] = -0.940741;
    CameraParameters[2] = 1.14751e-005;
    CameraParameters[3] = 1.14709e-005;
    CameraParameters[4] = 891.283;
    CameraParameters[5] = 943.919;
    CameraParameters[6] = 2000;
    CameraParameters[7] = 1500;*/

//    GenRadialDistortionMap(&Map, CameraParameters, CameraParameters, "bilinear");
}
int Measure_Sector::M_Sector(HObject Img, HTuple ModelID,HTuple &OutPara, HObject &OutImg)
{


    //换算比值
    float para1 = m_part.para1;
    //总体补偿值
    float para2 = m_part.para2;

    //弧边1--------------------
    //补偿值
    float para3 = m_part.para3;
    //标准值
    float para4 = m_part.para4;
    //上限值
    float para5 = m_part.para5;
    //下限值
    float para6 = m_part.para6;

    //弧边2------------------------
    //补偿值
    float para7 = m_part.para7;
    //标准值
    float para8 = m_part.para8;
    //上限值
    float para9 = m_part.para9;
    //下限值
    float para10 = m_part.para10;

    //直边3----------------------
    //补偿值
    float para11 = m_part.para11;
    //标准值
    float para12 = m_part.para12;
    //上限值
    float para13 = m_part.para13;
    //下限值
    float para14 = m_part.para14;

    //直边4----------------------
    //补偿值
    float para15 = m_part.para15;
    //标准值
    float para16 = m_part.para16;
    //上限值
    float para17 = m_part.para17;
    //下限值
    float para18 = m_part.para18;

    //中直边5----------------------
    //补偿值
    float para19 = m_part.para19;
    //标准值
    float para20 = m_part.para20;
    //上限值
    float para21 = m_part.para21;
    //下限值
    float para22 = m_part.para22;

    //图像处理--------
    //定位阈值
    float para23 = m_part.para23;





    /*

    //换算比值
    float para1=0.011468;
    //总体补偿值
    float para2=0.010326;

    //弧边1--------------------
    //补偿值
    float para3 = 0;
    //标准值
    float para4 = 2.18;
    //上限值
    float para5 = 0.01;
    //下限值
    float para6 = 0.01;

    //弧边2------------------------
    //补偿值
    float para7 = 0;
    //标准值
    float para8 = 0;
    //上限值
    float para9 = 1;
    //下限值
    float para10 = 1;

    //直边3----------------------
    //补偿值
    float para11 = 0;
    //标准值
    float para12 = 6.00;
    //上限值
    float para13 = 0.01;
    //下限值
    float para14 = 0.01;

    //直边4----------------------
    //补偿值
    float para15 = 0;
    //标准值
    float para16 = 6.00;
    //上限值
    float para17 = 0.01;
    //下限值
    float para18 = 0.01;

    //中直边5----------------------
    //补偿值
    float para19 = 0;
    //标准值
    float para20 = 6.00;
    //上限值
    float para21 = 0.01;
    //下限值
    float para22 = 0.01;

    //图像处理--------
    //定位阈值
    float para23 = 60;

    */






    Flag1 = true;
    Flag2 = true;
    Flag3 = true;
    Flag4 = true;
    Flag5 = true;
    Flag6 = true;

    /*大于上限值*/
    Flag11 = true;
    Flag22 = true;
    Flag33 = true;
    Flag44 = true;
    Flag55 = true;
    Flag66 = true;


    HObject  Image, ROI_0, ImageReduced;
    HObject  Region, ConnectedRegions, SelectedRegions;
    HObject  RegionTrans, Rectangle1, ImageReduced1;
    HObject  Region1, ConnectedRegions1, SelectedRegions1;
    HObject  Edges1, Rectangle2, ImageReduced2, Region2;
    HObject  ConnectedRegions2, SelectedRegions2, Edges2;
    HObject  ContCircle1, ContCircle2, MidLine;


    // Local control variables
    //HTuple  CameraParameters, CameraPose;
    HTuple  Width, Height, W, H, Row;
    HTuple  Column, Phi, Length1, Length2, Line1;
    HTuple  Line2, Index2, RowBegin, ColBegin, RowEnd;
    HTuple  ColEnd, Nr, Nc, Dist, MetrologyHandle;
    HTuple  Index1, Parameter, R, C, Radius;
    HTuple  StartPhi1, EndPhi1, PointOrder1, Min;
    HTuple  Min_Num, Max, Max_Num, Num1, Num2;
    HTuple  Num3, Num4, JR1, JC1, JR2, JC2;
    HTuple  JR3, JC3, JR4, JC4, Index3, MidLine1;
    HTuple  M_JR1, M_JC1, M_JR2, M_JC2, Num5;
    HTuple  Num6, Index4, D5, D1, D2, D3, D4;
    HTuple  DD1, DD2, DD3, DD4, DD5,Profile,Number;
    Number=0;
    ///轮廓度

    HObject Contour1,Contour2;
    HTuple Row1,Col1,Row2, Col2, Len;
    HTuple CR3, CC1, Phi1, Length11, Length21;
    HTuple Max1,Max2,MaxC,Index6,f;
    HTuple Row5,Row4,Column5,Column4;
    HTuple R4, C4, a, b, Indices, LineNum;
    HTuple JR_1, JC_1, JC_A1, JR_W1, JC_H1;
    HObject RegionLines1, ImageReduced11, Edges11, RegionLines2, ImageReduced22, Edges22;
    HTuple Row1111, Col1111, Length111, JR_2, JC_2, JC_A2, JR_W2, JC_H2, Length222, Row2222, Col2222, Index5;
    HObject RegionLines4,RegionLines3,Rectangle4,Rectangle3,ImageReduced44,ImageReduced33,Edges44,Edges33;
    HTuple Row3333,Row4444,Col3333,Col4444,Row6,Column6,Row7,Column7,RowBegin2, ColBegin2, RowEnd2, ColEnd2;
    HTuple RowBegin1, ColBegin1, RowEnd1, ColEnd1,IsOverlapping,JR_4, JC_4, JC_A4, JR_W4,JC_H4;
    HTuple JR_3, JC_3, JC_A3, JR_W3,JC_H3,c,d,Max3,Max4,Length333,Length444,Nr1, Nc1, Dist1,Nr2, Nc2, Dist2,Index7,Index8;
    HTuple P_Value=70;
    DD2.Append(0);
    DD3.Append(0);
    DD4.Append(0);
    DD5.Append(0);


    /*
    CameraParameters.Clear();
    CameraParameters[0] = 0;
    CameraParameters[1] = -0.940741;
    CameraParameters[2] = 1.14751e-005;
    CameraParameters[3] = 1.14709e-005;
    CameraParameters[4] = 891.283;
    CameraParameters[5] = 943.919;
    CameraParameters[6] = 2000;
    CameraParameters[7] = 1500;
    CameraPose.Clear();
    CameraPose[0] = -0.00709736;
    CameraPose[1] = -0.00755097;
    CameraPose[2] = 1.00222;
    CameraPose[3] = 357.262;
    CameraPose[4] = 359.28;
    CameraPose[5] = 0.546978;
    CameraPose[6] = 0;
    */


    int flag = 0;
    OutPara.Clear();
    try
    {
        OutImg = Img;

        GetImageSize(Img, &W, &H);
        if ((W != 2000) || (H != 1500))
        {
            OutPara.Append(0).Append(0).Append(0).Append(0).Append(0).Append(0);
            Flag1 = false;
            Flag2 = false;
            Flag3 = false;
            Flag4 = false;
            Flag5 = false;
            Flag6=false;
            return 2;
        }

        // GenRadialDistortionMap(&Map, CameraParameters, CameraParameters, "bilinear");
        MapImage(Img, Map, &Image);
        GetImageSize(Image, &Width, &Height);

        GenRectangle1(&ROI_0, m_part2.x1, m_part2.y1, m_part2.x2, m_part2.y2);

        //GenRectangle1(&ROI_0, 319.081, 204.297, 961.938, 1783.63);

        ReduceDomain(Image, ROI_0, &ImageReduced);
        Threshold(ImageReduced, &Region, 0, para23);
        Connection(Region, &ConnectedRegions);
        SelectShape(ConnectedRegions, &SelectedRegions, "area", "and", 10000, 9999999);
        CountObj(ConnectedRegions, &Number);
        if(!(Number.D() == 1))
        {
            OutPara.Append(0).Append(0).Append(0).Append(0).Append(0).Append(0);
            Flag1 = false;
            Flag2 = false;
            Flag3 = false;
            Flag4 = false;
            Flag5 = false;
            Flag6=false;
            return -1;
        }


//        SelectShapeStd(ConnectedRegions, &SelectedRegions, "max_area", 70);
        /*崩缺检测*/
        // Local iconic variables
        HObject  Region, ConnectedRegions;
        HObject  SxRegionDilation, SxRegionComplement;
        HObject  SxImageResult, SxModelContours, SxContoursAffineTrans;
        HObject  SxSelectedContours, SxRegion1, SxRegionDifference;
        HObject  SxRegionErosion, SxConnectedRegions1, SxSelectedRegions1;

        // Local control variables
        HTuple  SxModelID, SxRow, SxColumn, SxAngle;
        HTuple  SxScore, SxHomMat2DIdentity, SxHomMat2D, SxNumber;

        SxNumber = 0;
        SxModelContours.Clear();

        DilationRectangle1(SelectedRegions, &SxRegionDilation, 15, 15);
        Complement(SxRegionDilation, &SxRegionComplement);
        PaintRegion(SxRegionComplement, Image, &SxImageResult, 255, "fill");

        //		GetDeformableModelContours(&SxModelContours, ModelID, 1);

        //		FindPlanarUncalibDeformableModel(SxImageResult, ModelID, HTuple(0).TupleRad(), HTuple(360).TupleRad(),
        //			1, 1, 1, 1, 0.5, 0, 0.5, 7, 0.75, "subpixel", "least_squares_very_high", &SxHomMat2D,
        //			&SxScore);

        /*

    GetShapeModelContours(&SxModelContours, ModelID, 1);
        FindShapeModel(SxImageResult, ModelID, HTuple(0).TupleRad(), HTuple(360).TupleRad(),
            0.3, 0, 0.5, "least_squares", (HTuple(6).Append(1)), 0.75, &SxRow, &SxColumn,
            &SxAngle, &SxScore);

        if (0 != (SxScore > 0.5))
        {
            HomMat2dIdentity(&SxHomMat2DIdentity);
            VectorAngleToRigid(0, 0, 0, SxRow, SxColumn, SxAngle, &SxHomMat2D);
            AffineTransContourXld(SxModelContours, &SxContoursAffineTrans, SxHomMat2D);
//			ProjectiveTransContourXld(SxModelContours, &SxContoursAffineTrans, SxHomMat2D);
            UnionAdjacentContoursXld(SxContoursAffineTrans, &SxContoursAffineTrans, 10,
                1, "attr_keep");
            SelectContoursXld(SxContoursAffineTrans, &SxSelectedContours, "contour_length",
                30, 3000, -0.5, 0.5);
            GenRegionContourXld(SxSelectedContours, &SxRegion1, "filled");
            Difference(SxRegion1, SelectedRegions, &SxRegionDifference);
            ErosionRectangle1(SxRegionDifference, &SxRegionErosion, 5, 5);
            Connection(SxRegionErosion, &SxConnectedRegions1);
            SelectShape(SxConnectedRegions1, &SxSelectedRegions1, "area", "and", 50, 99999);
            CountObj(SxSelectedRegions1, &SxNumber);
            if (SxNumber.D() > 0)
            {
                OutPara.Append(0).Append(0).Append(0).Append(0).Append(0).Append(0);
                Flag1 = false;
                Flag2 = false;
                Flag3 = false;
                Flag4 = false;
                Flag5 = false;
                Flag6 = false;
                return 1;
            }
        }
*/
        /*----*/



        ShapeTrans(SelectedRegions, &RegionTrans, "rectangle2");
        RegionFeatures(RegionTrans, "width", &W);
        RegionFeatures(RegionTrans, "height", &H);
        SmallestRectangle2(RegionTrans, &Row, &Column, &Phi, &Length1,
                           &Length2);

        Line1 = HTuple();
        Line2 = HTuple();

        //两条直边
        //GenRectangle2(&Rectangle1, Row, Column, Phi, (W / 2) + 50, H / 3);
        GenRectangle2(&Rectangle1, Row, Column, Phi, (W / 2) + 50, H / 4);
        ReduceDomain(Image, Rectangle1, &ImageReduced1);
        Threshold(ImageReduced1, &Region1, 0, para23);
        Connection(Region1, &ConnectedRegions1);
        SelectShapeStd(ConnectedRegions1, &SelectedRegions1, "max_area", 70);
        DilationRectangle1(SelectedRegions1, &SelectedRegions1, 10, 10);
        ReduceDomain(Image, SelectedRegions1, &ImageReduced1);
        EdgesSubPix(ImageReduced1, &Edges1, "canny", 2, 20, 40);
        SelectContoursXld(Edges1, &Edges1, "contour_length", 150, 500, -0.5, 0.5);

        for (Index2 = 0; Index2 <= 1; Index2 += 1)
        {

            FitLineContourXld(Edges1, "tukey", -1, 0, 5, 2, &RowBegin, &ColBegin,
                              &RowEnd, &ColEnd, &Nr, &Nc, &Dist);
            CountObj(Edges1, &LineNum);
            if (LineNum.D() != 2)
            {
                OutPara.Append(0).Append(0).Append(0).Append(0).Append(0).Append(0);
                Flag1 = false;
                Flag2 = false;
                Flag3 = false;
                Flag4 = false;
                Flag5 = false;
                Flag6 = false;
                return 2;
            }
            CreateMetrologyModel(&MetrologyHandle);
            AddMetrologyObjectGeneric(MetrologyHandle, "line", ((HTuple(RowBegin[Index2]).TupleConcat(HTuple(ColBegin[Index2]))).TupleConcat(HTuple(RowEnd[Index2]))).TupleConcat(HTuple(ColEnd[Index2])),
                                      20, 5, 3, para23, HTuple(), HTuple(), &Index1);
            ApplyMetrologyModel(Image, MetrologyHandle);
            GetMetrologyObjectResult(MetrologyHandle, "all", "all", "result_type",
                                     "all_param", &Parameter);
            ClearMetrologyModel(MetrologyHandle);
            if (0 != (Index2 == 0))
            {
                Line1 = Parameter;
            }
            else
            {
                Line2 = Parameter;
            }

        }

        //两条弧边
        GenRectangle2(&Rectangle2, Row, Column, Phi, W / 4, (H / 2) + 50);
        //GenRectangle2(&Rectangle2, Row, Column, Phi, W / 2-35, (H / 2) + 50);
        double  aa = Phi.D();
        ReduceDomain(Image, Rectangle2, &ImageReduced2);
        Threshold(ImageReduced2, &Region2, 0, para23);
        Connection(Region2, &ConnectedRegions2);
        SelectShapeStd(ConnectedRegions2, &SelectedRegions2, "max_area", 70);
        DilationRectangle1(SelectedRegions2, &SelectedRegions2, 10, 10);
        ReduceDomain(Image, SelectedRegions2, &ImageReduced2);

        EdgesSubPix(ImageReduced2, &Edges2, "canny", 3, 20, 40);
        SelectContoursXld(Edges2, &Edges2, "contour_length", 150, 1000, -0.5, 0.5);

        //轮廓度
        SelectObj(Edges2, &Contour1, 1);
        GetContourXld(Contour1, &Row1, &Col1);
        TupleLength(Row1, &Length1);
        SelectObj(Edges2, &Contour2, 2);
        GetContourXld(Contour2, &Row2, &Col2);
        TupleLength(Row2, &Length2);
        TupleMin2(Length1, Length2, &Len);
        SmallestRectangle2Xld(Contour2, &CR3, &CC1, &Phi1, &Length11,
                              &Length21);
        TupleAbs(Phi1, &Phi1);




        FitCircleContourXld(Edges2, "geometric", -1, 0, 0, 3, 2, &R, &C, &Radius,
                            &StartPhi1, &EndPhi1, &PointOrder1);
        TupleMin(Radius, &Min);
        TupleFind(Radius, Min, &Min_Num);
        TupleMax(Radius, &Max);
        TupleFind(Radius, Max, &Max_Num);
        GenCircleContourXld(&ContCircle1, HTuple(R[Min_Num]), HTuple(C[Min_Num]),
                            HTuple(Radius[Min_Num]), -PI, PI, "positive", 1);
        GenCircleContourXld(&ContCircle2, HTuple(R[Max_Num]), HTuple(C[Max_Num]),
                            HTuple(Radius[Max_Num]), -PI, PI, "positive", 1);



        Num1 = HTuple();
        Num2 = HTuple();
        Num3 = HTuple();
        Num4 = HTuple();

        IntersectionLineCircle(HTuple(Line1[0]), HTuple(Line1[1]), HTuple(Line1[2]),
                HTuple(Line1[3]), HTuple(R[Min_Num]), HTuple(C[Min_Num]),
                HTuple(Radius[Min_Num]), -PI, PI, "positive", &JR1, &JC1);
        IntersectionLineCircle(HTuple(Line2[0]), HTuple(Line2[1]), HTuple(Line2[2]),
                HTuple(Line2[3]), HTuple(R[Min_Num]), HTuple(C[Min_Num]),
                HTuple(Radius[Min_Num]), -PI, PI, "positive", &JR2, &JC2);
        IntersectionLineCircle(HTuple(Line1[0]), HTuple(Line1[1]), HTuple(Line1[2]),
                HTuple(Line1[3]), HTuple(R[Max_Num]), HTuple(C[Max_Num]),
                HTuple(Radius[Max_Num]), -PI, PI, "positive", &JR3, &JC3);
        IntersectionLineCircle(HTuple(Line2[0]), HTuple(Line2[1]), HTuple(Line2[2]),
                HTuple(Line2[3]), HTuple(R[Max_Num]), HTuple(C[Max_Num]),
                HTuple(Radius[Max_Num]), -PI, PI, "positive", &JR4, &JC4);


        for (Index3 = 0; Index3 <= 1; Index3 += 1)
        {
            if (0 != (HTuple(HTuple(HTuple(HTuple(JR1[Index3]) > 0).TupleAnd(HTuple(JR1[Index3])<Width)).TupleAnd(HTuple(JC1[Index3])>0)).TupleAnd(HTuple(JC1[Index3])<Height)))
            {
                Num1 = Index3;
            }
            if (0 != (HTuple(HTuple(HTuple(HTuple(JR2[Index3])>0).TupleAnd(HTuple(JR2[Index3])<Width)).TupleAnd(HTuple(JC2[Index3])>0)).TupleAnd(HTuple(JC2[Index3])<Height)))
            {
                Num2 = Index3;
            }
            if (0 != (HTuple(HTuple(HTuple(HTuple(JR3[Index3])>0).TupleAnd(HTuple(JR3[Index3])<Width)).TupleAnd(HTuple(JC3[Index3])>0)).TupleAnd(HTuple(JC3[Index3])<Height)))
            {
                Num3 = Index3;
            }
            if (0 != (HTuple(HTuple(HTuple(HTuple(JR4[Index3])>0).TupleAnd(HTuple(JR4[Index3])<Width)).TupleAnd(HTuple(JC4[Index3])>0)).TupleAnd(HTuple(JC4[Index3]) < Height)))
            {
                Num4 = Index3;
            }

        }


        GenRegionLine(&MidLine, (HTuple(JR1[Num1]) + HTuple(JR2[Num2])) / 2,
                      (HTuple(JC1[Num1]) + HTuple(JC2[Num2])) / 2, (HTuple(JR3[Num3]) + HTuple(JR4[Num4])) / 2,
                      (HTuple(JC3[Num3]) + HTuple(JC4[Num4])) / 2);
        MidLine1.Clear();
        MidLine1.Append((HTuple(JR1[Num1]) + HTuple(JR2[Num2])) / 2);
        MidLine1.Append((HTuple(JC1[Num1]) + HTuple(JC2[Num2])) / 2);
        MidLine1.Append((HTuple(JR3[Num3]) + HTuple(JR4[Num4])) / 2);
        MidLine1.Append((HTuple(JC3[Num3]) + HTuple(JC4[Num4])) / 2);
        IntersectionLineCircle(HTuple(MidLine1[0]), HTuple(MidLine1[1]), HTuple(MidLine1[2]),
                HTuple(MidLine1[3]), HTuple(R[0]), HTuple(C[0]), HTuple(Radius[0]),
                -PI, PI, "positive", &M_JR1, &M_JC1);
        IntersectionLineCircle(HTuple(MidLine1[0]), HTuple(MidLine1[1]), HTuple(MidLine1[2]),
                HTuple(MidLine1[3]), HTuple(R[1]), HTuple(C[1]), HTuple(Radius[1]),
                -PI, PI, "positive", &M_JR2, &M_JC2);

        Num5 = HTuple();
        Num6 = HTuple();
        for (Index4 = 0; Index4 <= 1; Index4 += 1)
        {
            if (0 != (HTuple(HTuple(HTuple(HTuple(M_JR1[Index4]) > 0).TupleAnd(HTuple(M_JR1[Index4])<Width)).TupleAnd(HTuple(M_JC1[Index4])>0)).TupleAnd(HTuple(M_JC1[Index4])<Width)))
            {
                Num5 = Index4;
            }
            if (0 != (HTuple(HTuple(HTuple(HTuple(M_JR2[Index4])>0).TupleAnd(HTuple(M_JR2[Index4])<Width)).TupleAnd(HTuple(M_JC2[Index4])>0)).TupleAnd(HTuple(M_JC2[Index4]) < Width)))
            {
                Num6 = Index4;
            }

        }
        /*
        //轮廓度

        Max1 = HTuple();
        Max2 = HTuple();
        {
            HTuple end_val167 = (Len-1)/20;
            HTuple step_val167 = 1;
            for (Index6=0; Index6.Continue(end_val167, step_val167); Index6 += step_val167)
            {
                f = Index6*20;
                if (0 != (Phi1<(PI/4)))
                {
                    IntersectionLineCircle(HTuple(Row1[f]), HTuple(Col1[f]), HTuple(Row1[f])+100,
                                           HTuple(Col1[f]), HTuple(R[0]), HTuple(C[0]), HTuple(Radius[0]),
                            -PI, PI, "positive", &Row4, &Column4);
                    IntersectionLineCircle(HTuple(Row2[f]), HTuple(Col2[f]), HTuple(Row2[f])+100,
                                           HTuple(Col2[f]), HTuple(R[1]), HTuple(C[1]), HTuple(Radius[1]),
                            -PI, PI, "positive", &Row5, &Column5);
                    TupleAbs(Row4, &R4);
                    TupleMin(R4, &R4);
                    TupleFind(Row4, R4, &Indices);
                    a = HTuple(Row1[f])-HTuple(Row4[Indices]);
                    b = HTuple(Row2[f])-HTuple(Row5[Indices]);
                    TupleAbs(a, &a);
                    TupleAbs(b, &b);
                    TupleConcat(Max1, a, &Max1);
                    TupleConcat(Max2, b, &Max2);
                }
                else
                {
                    IntersectionLineCircle(HTuple(Row1[f]), HTuple(Col1[f]), HTuple(Row1[f]),
                                           HTuple(Col1[f])+100, HTuple(R[0]), HTuple(C[0]), HTuple(Radius[0]),
                            -PI, PI, "positive", &Row4, &Column4);
                    IntersectionLineCircle(HTuple(Row2[f]), HTuple(Col2[f]), HTuple(Row2[f]),
                                           HTuple(Col2[f])+100, HTuple(R[1]), HTuple(C[1]), HTuple(Radius[1]),
                            -PI, PI, "positive", &Row5, &Column5);
                    TupleAbs(Column4, &C4);
                    TupleMin(C4, &C4);
                    TupleFind(Column4, C4, &Indices);
                    a = HTuple(Col1[f])-HTuple(Column4[Indices]);
                    b = HTuple(Col2[f])-HTuple(Column5[Indices]);
                    TupleAbs(a, &a);
                    TupleAbs(b, &b);
                    TupleConcat(Max1, a, &Max1);
                    TupleConcat(Max2, b, &Max2);
                }

            }
        }
        TupleConcat(Max1, Max2, &MaxC);
        TupleMax(MaxC, &MaxC);
        Profile = ((MaxC*para1)+para2)*2;
        if(Profile>0.08)
        {
            Flag6=false;
            flag = 1;
        }
        */


        GenRegionLine(&RegionLines1, HTuple(JR1[Num1]), HTuple(JC1[Num1]),
                      HTuple(JR2[Num2]), HTuple(JC2[Num2]));
        SmallestRectangle2(RegionLines1, &JR_1, &JC_1, &JC_A1, &JR_W1,&JC_H1);
        GenRectangle2(&Rectangle1, JR_1, JC_1, JC_A1, JR_W1 - 10, JC_H1 + 20);
        ReduceDomain(Image, Rectangle1, &ImageReduced11);
        EdgesSubPix(ImageReduced11, &Edges11, "canny", 4, 20, 40);
        //        ThresholdSubPix(ImageReduced11, &Edges11, P_Value);
        SelectContoursXld(Edges11, &Edges11, "contour_length", 250, 400, -0.5,0.5);
        GetContourXld(Edges11, &Row1111, &Col1111);
        TupleLength(Row1111, &Length111);



        GenRegionLine(&RegionLines2, HTuple(JR3[Num3]), HTuple(JC3[Num3]),
                      HTuple(JR4[Num4]), HTuple(JC4[Num4]));
        SmallestRectangle2(RegionLines2, &JR_2, &JC_2, &JC_A2, &JR_W2,
                           &JC_H2);
        GenRectangle2(&Rectangle2, JR_2, JC_2, JC_A2, JR_W2 - 10, JC_H2 + 20);

        ReduceDomain(Image, Rectangle2, &ImageReduced22);
        EdgesSubPix(ImageReduced22, &Edges22, "canny", 4, 20, 40);
        // ThresholdSubPix(ImageReduced22, &Edges22, P_Value);
        SelectContoursXld(Edges22, &Edges22, "contour_length", 250, 400, -0.5,
                          0.5);
        GetContourXld(Edges22, &Row2222, &Col2222);
        TupleLength(Col2222, &Length222);



        GenRegionLine(&RegionLines3, HTuple(JR1[Num1]), HTuple(JC1[Num1]),
                      HTuple(JR3[Num3]), HTuple(JC3[Num3]));
        SmallestRectangle2(RegionLines3, &JR_3, &JC_3, &JC_A3, &JR_W3,
                           &JC_H3);
        GenRectangle2(&Rectangle3, JR_3, JC_3, JC_A3, JR_W3-10, JC_H3+20);
        ReduceDomain(Image, Rectangle3, &ImageReduced33);

        //        ThresholdSubPix(ImageReduced33, &Edges33, P_Value);
        EdgesSubPix(ImageReduced33, &Edges33, "canny", 4, 20, 40);

        SelectContoursXld(Edges33, &Edges33, "contour_length", 250, 400, -0.5,
                          0.5);
        GetContourXld(Edges33, &Row3333, &Col3333);
        TupleLength(Row3333, &Length333);
        FitLineContourXld(Edges33, "tukey", -1, 0, 5, 2, &RowBegin1, &ColBegin1,
                          &RowEnd1, &ColEnd1, &Nr1, &Nc1, &Dist1);





        GenRegionLine(&RegionLines4, HTuple(JR2[Num2]), HTuple(JC2[Num2]),
                      HTuple(JR4[Num4]), HTuple(JC4[Num4]));
        SmallestRectangle2(RegionLines4, &JR_4, &JC_4, &JC_A4, &JR_W4,
                           &JC_H4);
        GenRectangle2(&Rectangle4, JR_4, JC_4, JC_A4, JR_W4-10, JC_H4+20);
        ReduceDomain(Image, Rectangle4, &ImageReduced44);
        //        ThresholdSubPix(ImageReduced44, &Edges44, P_Value);
        EdgesSubPix(ImageReduced44, &Edges44, "canny", 4, 20, 40);
        SelectContoursXld(Edges44, &Edges44, "contour_length", 250, 400, -0.5,
                          0.5);
        GetContourXld(Edges44, &Row4444, &Col4444);
        TupleLength(Row4444, &Length444);
        FitLineContourXld(Edges44, "tukey", -1, 0, 5, 2, &RowBegin2, &ColBegin2,
                          &RowEnd2, &ColEnd2, &Nr2, &Nc2, &Dist2);




        Max1 = HTuple();
        Max2 = HTuple();
        Max3 = HTuple();
        Max4 = HTuple();
        if (0 != (Phi1 < (PI / 4)))
        {

            {
                HTuple end_val196 = (Length222 - 1) / 10;
                HTuple step_val196 = 1;
                for (Index5 = 0; Index5.Continue(end_val196, step_val196); Index5 += step_val196)
                {
                    f = Index5 * 10;
                    IntersectionLineCircle(HTuple(Row2222[f]), HTuple(Col2222[f]),
                                           HTuple(Row2222[f]) + 100, HTuple(Col2222[f]), HTuple(R[Max_Num]),
                                           HTuple(C[Max_Num]), HTuple(Radius[Max_Num]), -PI , PI ,
                                           "positive", &Row5, &Column5);

                    TupleAbs(Row5, &R4);
                    TupleMin(R4, &R4);
                    TupleFind(Row5, R4, &Indices);
                    b = HTuple(Row2222[f]) - HTuple(Row5[Indices]);
                    TupleAbs(b, &b);

                    TupleConcat(Max2, b, &Max2);
                }
            }

            {
                HTuple end_val210 = (Length111 - 1) / 10;
                HTuple step_val210 = 1;
                for (Index6 = 0; Index6.Continue(end_val210, step_val210); Index6 += step_val210)
                {
                    f = Index6 * 10;
                    IntersectionLineCircle(HTuple(Row1111[f]), HTuple(Col1111[f]),
                                           HTuple(Row1111[f]) + 100, HTuple(Col1111[f]), HTuple(R[Min_Num]),
                                           HTuple(C[Min_Num]), HTuple(Radius[Min_Num]), -PI , PI ,
                                           "positive", &Row4, &Column4);


                    TupleAbs(Row4, &R4);
                    TupleMin(R4, &R4);
                    TupleFind(Row4, R4, &Indices);
                    a = HTuple(Row1111[f]) - HTuple(Row4[Indices]);
                    TupleAbs(a, &a);

                    TupleConcat(Max1, b, &Max1);
                }
            }
            {
                HTuple end_val268 = (Length333-1)/10;
                HTuple step_val268 = 1;
                for (Index7=0; Index7.Continue(end_val268, step_val268); Index7 += step_val268)
                {
                    f = Index7*10;
                    IntersectionLines(HTuple(Row3333[f]), HTuple(Col3333[f]), HTuple(Row3333[f]),
                                      HTuple(Col3333[f])+100, RowBegin1, ColBegin1, RowEnd1,
                                      ColEnd1, &Row6, &Column6, &IsOverlapping);

                    c = HTuple(Col3333[f])-Column6;
                    TupleAbs(c, &c);
                    TupleConcat(Max3, c, &Max3);
                }
            }

            {
                HTuple end_val277 = (Length444-1)/10;
                HTuple step_val277 = 1;
                for (Index8=0; Index8.Continue(end_val277, step_val277); Index8 += step_val277)
                {
                    f = Index8*10;
                    IntersectionLines(HTuple(Row4444[f]), HTuple(Col4444[f]), HTuple(Row4444[f]),
                                      HTuple(Col4444[f])+100, RowBegin2, ColBegin2, RowEnd2,
                                      ColEnd2, &Row7, &Column7, &IsOverlapping);

                    d = HTuple(Col4444[f])-Column7;
                    TupleAbs(d, &d);
                    TupleConcat(Max4, d, &Max4);
                }
            }


        }
        else
        {
            {
                HTuple end_val227 = (Length222 - 1) / 10;
                HTuple step_val227 = 1;
                for (Index5 = 0; Index5.Continue(end_val227, step_val227); Index5 += step_val227)
                {
                    f = Index5 * 10;
                    IntersectionLineCircle(HTuple(Row2222[f]), HTuple(Col2222[f]),
                                           HTuple(Row2222[f]), HTuple(Col2222[f]) + 100, HTuple(R[Max_Num]),
                                           HTuple(C[Max_Num]), HTuple(Radius[Max_Num]), -PI , PI ,
                                           "positive", &Row5, &Column5);


                    TupleAbs(Column5, &C4);
                    TupleMin(C4, &C4);
                    TupleFind(Column5, C4, &Indices);
                    b = HTuple(Col2222[f]) - HTuple(Column5[Indices]);
                    TupleAbs(b, &b);

                    TupleConcat(Max2, b, &Max2);
                }
            }

            {
                HTuple end_val241 = (Length111 - 1) / 10;
                HTuple step_val241 = 1;
                for (Index6 = 0; Index6.Continue(end_val241, step_val241); Index6 += step_val241)
                {
                    f = Index6 * 10;
                    IntersectionLineCircle(HTuple(Row1111[f]), HTuple(Col1111[f]),
                                           HTuple(Row1111[f]), HTuple(Col1111[f]) + 100, HTuple(R[Min_Num]),
                                           HTuple(C[Min_Num]), HTuple(Radius[Min_Num]), -PI , PI ,
                                           "positive", &Row4, &Column4);

                    TupleAbs(Column4, &C4);
                    TupleMin(C4, &C4);
                    TupleFind(Column4, C4, &Indices);
                    a = HTuple(Col1111[f]) - HTuple(Column4[Indices]);
                    TupleAbs(a, &a);

                    TupleConcat(Max1, a, &Max1);
                }
            }
            {
                HTuple end_val317 = (Length333-1)/10;
                HTuple step_val317 = 1;
                for (Index7=0; Index7.Continue(end_val317, step_val317); Index7 += step_val317)
                {
                    f = Index7*10;
                    IntersectionLines(HTuple(Row3333[f]), HTuple(Col3333[f]), HTuple(Row3333[f])+100,
                                      HTuple(Col3333[f]), RowBegin1, ColBegin1, RowEnd1, ColEnd1,
                                      &Row6, &Column6, &IsOverlapping);

                    c = HTuple(Row3333[f])-Row6;
                    TupleAbs(c, &c);
                    TupleConcat(Max3, c, &Max3);
                }
            }

            {
                HTuple end_val326 = (Length444-1)/10;
                HTuple step_val326 = 1;
                for (Index8=0; Index8.Continue(end_val326, step_val326); Index8 += step_val326)
                {
                    f = Index8*10;
                    IntersectionLines(HTuple(Row4444[f]), HTuple(Col4444[f]), HTuple(Row4444[f])+100,
                                      HTuple(Col4444[f]), RowBegin2, ColBegin2, RowEnd2, ColEnd2,
                                      &Row7, &Column7, &IsOverlapping);

                    d = HTuple(Row4444[f])-Row7;
                    TupleAbs(d, &d);
                    TupleConcat(Max4, d, &Max4);
                }
            }

        }
        TupleConcat(Max1, Max2, &Max1);
        TupleConcat(Max1, Max3, &Max1);
        TupleConcat(Max1, Max4, &MaxC);
        TupleMax(MaxC, &MaxC);
        Profile = (MaxC*para1) * 2;
        if (Profile > 0.08)
        {
            Flag6 = false;
            flag = 1;
        }














        DistancePp(HTuple(M_JR1[Num5]), HTuple(M_JC1[Num5]), HTuple(M_JR2[Num6]),
                   HTuple(M_JC2[Num6]), &D5);
        DistancePp(HTuple(JR1[Num1]), HTuple(JC1[Num1]), HTuple(JR2[Num2]),
                   HTuple(JC2[Num2]), &D1);
        DistancePp(HTuple(JR3[Num3]), HTuple(JC3[Num3]), HTuple(JR4[Num4]),
                   HTuple(JC4[Num4]), &D2);
        DistancePp(HTuple(JR1[Num1]), HTuple(JC1[Num1]), HTuple(JR3[Num3]),
                   HTuple(JC3[Num3]), &D3);
        DistancePp(HTuple(JR2[Num2]), HTuple(JC2[Num2]), HTuple(JR4[Num4]),
                   HTuple(JC4[Num4]), &D4);

        //弧边1
        DD1 = D1*para1 + para2 + para3;
        //弧边2
        DD2 = D2*para1 + para2 + para7;
        //直边3
        DD3 = D3*para1 + para2 + para11;
        //直边4
        DD4 = D4*para1 + para2 + para15;
        //中直边5
        DD5 = D5*para1 + para2 + para19;


        if((DD1<(para4 + 0.1)) && (DD1>(para4 - 0.1))&&(DD2<(para8 + 0.1)) && (DD2>(para8 - 0.1))&&(DD3<(para12 + 0.1)) && (DD3>(para12 - 0.1))
                &&(DD4<(para16 + 0.1)) && (DD4>(para16 - 0.1))&&(DD5<(para20 + 0.1)) && (DD5>(para20 - 0.1)))
        {
            qDebug()<<"111";
            if (!((DD1<(para4 + para5)) && (DD1>(para4 - para6))))
            {
                if (DD1 < (para4 - para6))
                {
                    Flag11 = false;
                }
                Flag1 = false;
                flag = 1;
            }
            if (!((DD2<(para8 + para9)) && (DD2>(para8 - para10))))
            {
                if (DD2 < (para8 - para10))
                {
                    Flag22 = false;
                }
                Flag2 = false;
                flag = 1;
            }
            if (!((DD3<(para12 + para13)) && (DD3>(para12 - para14))))
            {
                if (DD3<(para12 - para14))
                {
                    Flag33 = false;
                }
                Flag3 = false;
                flag = 1;
            }
            if (!((DD4<(para16 + para17)) && (DD4>(para16 - para18))))
            {
                if (DD4 < (para16 - para18))
                {
                    Flag44 = false;
                }
                Flag4 = false;
                flag = 1;
            }
            if (!((DD5<(para20 + para21)) && (DD5>(para20 - para22))))
            {
                if (DD5<(para20 - para22))
                {
                    Flag55 = false;
                }
                Flag5 = false;
                flag = 1;
            }
        }
        else
        {
            Flag1 = false;
            Flag2 = false;
            Flag3 = false;
            Flag4 = false;
            Flag5 = false;
            flag = -1;
        }
        qDebug()<<QString::number(flag)<<"222";
        TupleConcat(OutPara, DD1, &OutPara);
        TupleConcat(OutPara, DD2, &OutPara);
        TupleConcat(OutPara, DD3, &OutPara);
        TupleConcat(OutPara, DD4, &OutPara);
        TupleConcat(OutPara, DD5, &OutPara);
        TupleConcat(OutPara, Profile, &OutPara);
        return flag;
    }
    catch (HException ex)
    {
        OutPara.Append(0).Append(0).Append(0).Append(0).Append(0).Append(0);
        Flag1 = false;
        Flag2 = false;
        Flag3 = false;
        Flag4 = false;
        Flag5 = false;
        Flag6 = false;
        
        return 2;
    }



}
